<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Dialog } from '@/components/Dialog'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTag } from 'element-plus'
import { Table } from '@/components/Table'
import {getViewApi,getDelViewApi,getEditViewApi } from '@/api/table'
import { useTable } from '@/views/Example/Dialog/useTable'
import { AdvertDemo } from '@/api/table/types'
import { h, ref, unref, reactive } from 'vue'
import Write from './components/Write.vue'
import Detail from './components/Detail.vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'
import {push} from "echarts/types/src/component/dataZoom/history";
import {data} from "autoprefixer";

const { register, tableObject, methods } = useTable<AdvertDemo>({
  getListApi: getViewApi,
  delListApi: getDelViewApi,
  getDelApi:getEditViewApi,
  response: {
    list: 'list',
    total: 'total'
  },
  defaultParams: {
    title: 's'
  },
})

const { getList, setSearchParams } = methods

getList()

const { t } = useI18n()
//编辑
const DeiSchemas = reactive<CrudSchema[]>([

  {
    field:'img_title',
    label:t('轮播图名称'),
    component:'Input',
  },
  {
    field:'img_address',
    label:t('轮播图地址'),
    component:'Input',
  },
  //轮播图类型
  {
    field: 'img_type',
    label: t('轮播图类型'),
    component: 'Select',
    formItemProps: {
      //rules: [required()]
    },
    componentProps: {
      options: [
        {
          label: '商品',
          value: 1
        },
        {
          label: '视频',
          value: 2
        },
      ]
    }
  },
  //开始时间
  {
    field: 'start_date',
    component: 'DatePicker',
    label: t('formDemo.in_time'),
    componentProps: {
      type: 'date',
      format: 'YYYY/MM/DD',
      valueFormat: 'YYYY-MM-DD',
      slots: {
        default: true
      }
    }
  },
  //上线/下线
  {
    field: 'display',
    label: t('formDemo.in_ty'),
    component: 'Radio',
    componentProps: {
      options: [
        {
          label: '上线',
          value: '1'
        },
        {
          label: '下线',
          value: '2'
        }
      ]
    }
  },
  //备注
  {
    field: 'remarks',
    label: '备注',
    component: 'Input',
  },
])
const crudSchemas = reactive<CrudSchema[]>([
  {
    field:'carousel_id',
    label:t('AdvertDemo.accID')
  },
  {
    field:'img_address',
    label:t('AdvertDemo.accSize')
  },
  {
    field:'create_user',
    label:t('AdvertDemo.accRole')
  },
  {
    field:'img_title',
    label:t('AdvertDemo.accCommpre')
  },
  {
    field: 'img_type',
    label: t('AdvertDemo.accType'),
    formatter: (row:AdvertDemo) => {
      return h(
              'span',
              row.img_type === 2 ? t('视频') : (row.img_type > 2 ? t('其他') : t('商品'))
      )
    }
  },
  {
    field:'remarks',
    label:t('AdvertDemo.accBize')
  },
  {
    field:'update_date',
    label:t('AdvertDemo.accTiem')
  },
  {
    field: 'action',
    label: t('tableDemo.action')
  }
])
const { allSchemas } = useCrudSchemas(crudSchemas)

const dialogVisible = ref(false)

const dialogTitle = ref('')

const delLoading = ref(false)

const delData = async (row: AdvertDemo | null, multiple: boolean) => {
   tableObject.currentRow = row
   console.log("方法执行之千")
   console.log(row.carousel_id)
   const { delList, getSelections } = methods
   const selections = await getSelections()
   delLoading.value = true
   await delList(
     multiple ? selections.map((v) => v.carousel_id) : [tableObject.currentRow?.carousel_id as number],
     multiple
   ).finally(() => {
     delLoading.value = false
   })
}

const actionType = ref('')

const action = (row: AdvertDemo, type: string) => {
  dialogTitle.value = t(type === 'edit' ? 'exampleDemo.edit' : 'exampleDemo.detail')
  actionType.value = type
  tableObject.currentRow = row
  dialogVisible.value = true
}

const writeRef = ref<ComponentRef<typeof Write>>()

const loading = ref(false)

const save = async () => {
  const write = unref(writeRef)
  await write?.elFormRef?.validate(async (isValid) => {
    if (isValid) {
      loading.value = true
      const data = (await write?.getFormData()) as AdvertDemo
      data.carouselId = data.carousel_id
      data.imgTitle = data.img_title
      data.imgType = data.img_type
      // data.startDate = data.start_date
      data.imgAddress = data.img_address
      console.log(data.imgTitle)
      console.log(data)
      const res = await getEditViewApi(data)
        .catch(() => {})
        .finally(() => {
          loading.value = false
        })
      if (res) {
        dialogVisible.value = false
        tableObject.currentPage = 1
        getList()
      }
    }
  })
}

</script>

<template>
  <ContentWrap :title="t('tableDemo.table')" :message="t('tableDemo.tableDes')">
    <Table
            v-model:pageSize="tableObject.pageSize"
            v-model:currentPage="tableObject.currentPage"
            :columns="allSchemas.tableColumns"
            :data="tableObject.tableList.slice(
          (tableObject.currentPage - 1) * tableObject.pageSize,
          tableObject.currentPage * tableObject.pageSize)"
            :loading="tableObject.loading"
            :pagination="{
        total: tableObject.total
      }"
            @register="register"
    >
      <template #action="{ row }">
        <ElButton type="primary" v-hasPermi="['example:dialog:edit']" @click="action(row, 'edit')">
          {{ t('exampleDemo.edit') }}
        </ElButton>
        <ElButton type="danger" v-hasPermi="['example:dialog:delete']" @click="delData(row, false)">
          {{ t('exampleDemo.del') }}
        </ElButton>
      </template>
<!-- 图片展示-->
      <template #img_address="{ row }:{ row : AdvertDemo }">
        <img :src="row.img_address" width="70" height="70">
      </template>
    </Table>
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <Write
            v-if="actionType !== 'detail'"
            ref="writeRef"
            :form-schema="DeiSchemas"
            :current-row="tableObject.currentRow"
    />

    <Detail
            v-if="actionType === 'detail'"
            :detail-schema="allSchemas.detailSchema"
            :current-row="tableObject.currentRow"
    />

    <template #footer>
      <ElButton v-if="actionType !== 'detail'" type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
      <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
    </template>

  </Dialog>

</template>
